﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services.Protocols;
using System.Text;
using DispatchAssist.BusinessLogical.BLL;
using DispatchAssist.BusinessLogical.Model;

namespace DispatchAssist.WebService
{
    public class DASoapHeader : SoapHeader
    {
        static log4net.ILog Logger = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

        public string UserId { get; set; }
        public string RoleId { get; set; }
        public string[] SeatId { get; set; }

        public string SelfSeatID { get; set; }

        public string[] DelegateSeatIDs { get; set; }

        public bool ValidateUser()
        {
            //todo: 随后进行开发,finsh at 2011-8-10 by wuzhongqi
            if (!string.IsNullOrEmpty(this.UserId)&&SeatId!=null&&SeatId.Length>0)
            {

                try
                {
                    List<string> seatIDs = CommonCacheData.CacheLonginSeatInfo.GetRelatedSeatIDs(UserId);

                    return IsEffective(seatIDs);
                }
                catch
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }

        public void Log(string UserID, string SeatID, string MethodName, string[] param)
        {
            StringBuilder sbParams = new StringBuilder();
            param.ToList().ForEach(p => { sbParams.Append(p); sbParams.Append(","); });

            List<SystemLog> sysloglst = new List<SystemLog>();
            SystemLog syslog = new SystemLog();
            syslog.SeatID = new Guid(SeatID);
            syslog.UserID = new Guid(UserID);
            syslog.Method = MethodName;
            string strParams = sbParams.ToString();
            syslog.Parameters = string.IsNullOrEmpty(strParams) ? string.Empty : strParams.Substring(0, strParams.Length - 1);
            syslog.Description = string.Empty;
            syslog.LogContent = string.Empty;

            sysloglst.Add(syslog);
            SystemLogManager.InsertSystemLog(sysloglst);
        }

        private bool IsEffective(List<string> seatIDs)
        {
            if (seatIDs == null || seatIDs.Count == 0)
            {
                return false;
            }

            foreach (string seatID in seatIDs)
            {
                if (SeatId.Contains(seatID))
                {
                    return true;
                }
            }
            return false;
        } 

    }
}